package day06;

/**
 * 二维数组的使用
 *
 * @author Smartloe
 * @create 2021-04-23 17:54
 */
public class ArrayTest2 {
    public static void main(String[] args) {
        //1.二维数组的声明和初始化
        int[] arr = new int[]{1,2,3};//一维数组
        //静态初始化
        int[][] arr1 = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        //动态初始化1
        String[][] arr2 = new String[3][2];
        //动态初始化2
        String[][] arr3 = new String[3][];
        //错误情况
//        String[][] arr4 = new String[][4];
//        int[][] arr5 = new int[3][3]{{1,2,3},{4,5,6},{7,8,9}};
        //正确情况
        int[] arr6[] = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int arr7[][] = {{1,2,3},{4,5,6},{7,8,9}};//类型推断

        //2.如何调用数组的指定位置的元素
        System.out.println(arr1[0][1]);//2
        System.out.println(arr2[1][1]);//null

        arr3[1] = new String[4];
        System.out.println(arr3[1][0]);

        //3.获取数组长度
        System.out.println(arr1.length);
        System.out.println(arr1[0].length);

        //4.如何遍历二维数组
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j <arr1[i].length ; j++) {
                System.out.print(arr1[i][j]+" ");
            }
            System.out.println();

        }
    }
}
